<template>
    <div class="col-12">
        <div class="row mb-2" v-if="category !== ''">
            <div class="col-12">
                <h1>#{{ category }}</h1>
            </div>
        </div>
        <div class="row">
            <div class="col-md-9">
                <div class="playlist-container row">
                    <div class="col-12">
                        <div class="card-columns">
                            <div class="card border-0 shadow-sm mb-4" v-for="playlist in playlists" :key="playlist.id">
                                <router-link :to="{ name: 'playlist', params: { listId: playlist.id }}">
                                    <img class="card-img-top" v-bind:src="playlist.coverImgUrl" :alt="playlist.name"/>
                                    <div class="card-body w-100">
                                        <h4 class="card-title mb-0">{{ playlist.name }}</h4>
                                    </div>
                                </router-link>
                                <div class="card-footer border-0">
                                    <small class="mr-1" v-for="tag in playlist.tags" :key="tag">#{{ tag }}</small>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <ul class="list-group shadow-sm">
<!--                    <router-link class="list-group-item list-group-item-action border-0" to="/">默认</router-link>-->
                    <router-link class="list-group-item list-group-item-action border-0" v-for="category in categories" :key="category.id" :to="{ name: 'category', params: { category: category.name }}">
                        {{ category.name }}
                    </router-link>
                </ul>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "home",
        data() {
            return {
                playlists: [],
                categories: [],
                category: '',
            }
        },
        beforeMount() {
            this.getCategoriesData()
            this.getPlaylistsData('')
            this.category = ''
        },
        watch: {
            $route() {
                this.getPlaylistsData(this.$route.params.category)
                this.category = this.$route.params.category
            }
        },
        methods: {
            getPlaylistsData(category) {
                this.$http.get(`/api/top/playlist/highquality?cat=${category}`).then(response => {
                    this.playlists = response.data.playlists;
                })
            },
            getCategoriesData() {
                this.$http.get('/api/playlist/hot').then(response => {
                    this.categories = response.data.tags;
                })
            }
        }
    }
</script>